﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Comp476Project
{
    public enum TEAM
    {
        NONE,
        BLUE,
        RED,
        

    };
    public class Entity
    {

        public static Random rand = new Random();
        public Vector3 position, direction, defaultDirection;
        public List<Model>modelList;
        public List<Matrix> worldList;
        public Matrix projection;
        public float baseDamage;
        public bool boosted;


        public bool invincible, dead;


        public float rotAng, turnSpeed, size, health, maxHealth, attackRange, damage, viewRange;
       

        public TEAM myTeam;
        public float modelAng;

        
        

        public Entity target;

        public int attackTimer,damageTimer;
        

       

      
        public double deathTimer;
        Primitives3D.SpherePrimitive sphere;
        public Entity(Primitives3D.SpherePrimitive sphere,Matrix proj, Model mod, Vector3 pos ) 
        {
            damage = 0;
            
            target = null;
            projection = proj;
            modelList = new List<Model>();
            modelList.Add(mod);
            worldList = new List<Matrix>();
            rotAng = 0;
            turnSpeed = 0.04f;
            position = pos;
            modelAng = -90f;
            worldList.Add(Matrix.CreateScale(size)*Matrix.CreateRotationX(MathHelper.ToRadians(modelAng))*Matrix.CreateRotationY(rotAng)*Matrix.CreateTranslation(position));
            direction = new Vector3(0,0,1);
            defaultDirection = new Vector3(0,0,1);
            size = 0.01f;
            health = 0;
            dead= false;
            myTeam = TEAM.NONE;
            this.sphere = sphere;
        }
        public virtual void update(GameTime gameTime) 
        {

            computeOrientation();

            //for (int i = 0; i < worldList.Count; ++i)
            //{
            //    worldList[i] =Matrix.CreateScale(size)*Matrix.CreateRotationX(MathHelper.ToRadians(-90))*Matrix.CreateRotationY(rotAng)* Matrix.CreateTranslation(position);
            //}
          
          
            
        }

        public virtual void draw(Matrix view) 
        {
            for (int i = 0; i < modelList.Count;++i )
            {
                worldList[i] = Matrix.CreateScale(size) * Matrix.CreateRotationX(MathHelper.ToRadians(-90)) * Matrix.CreateRotationY(rotAng) * Matrix.CreateTranslation(position);
                DrawModel(modelList[i],worldList[i],view);
            }
        }


        private void DrawModel(Model model,Matrix world, Matrix view)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    //effect.DirectionalLight0.Direction = 1.5f * arena.sun.direction;
                    effect.DirectionalLight0.Direction = 1.0f * new Vector3(-1, -1, -1);
                    effect.PreferPerPixelLighting = true;
                    //effect.DirectionalLight0.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f);
                    //effect.DirectionalLight0.Direction = new Vector3(-1, -1, -1);
                    //effect.DirectionalLight0.SpecularColor = new Vector3(1.0f, 1.0f, 1.0f);
                    //effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
                    //effect.EmissiveColor = new Vector3(0.1f, 0.1f, 0.1f);
                    //effect.DirectionalLight0.Enabled = false;
                    //effect.LightingEnabled = true;
                    effect.World = world;
                    effect.View = view;
                    effect.Projection = projection;
                    //effect.Alpha = 0.7f;
                }

                mesh.Draw();
            }
        }

        private void computeOrientation() 
        {
            
            direction.Normalize();
           


            double angCos = Vector3.Dot(defaultDirection, direction);

            double tempAng = Math.Acos(angCos);
            rotAng = (float)tempAng;
            if (this.direction.X < 0)
                rotAng *= -1;
        }

        
    }
}
